import { _decorator, Component, Node } from 'cc';
import { EliminateUIManager } from './EliminateUIManager';
import { input } from 'cc';
import { Input } from 'cc';
import { EventTouch } from 'cc';
import { geometry } from 'cc';
import { audioManager } from '../../common/AudioManager';
import { PhysicsSystem } from 'cc';
import { RigidBody } from 'cc';
import { PhyGroup } from './EliminateSlot';
import { Vec3 } from 'cc';
import { EliminateToy } from './EliminateToy';
import { tween } from 'cc';
import { EliminateMatch3Logic } from './EliminateMatch3Logic';
const { ccclass, property } = _decorator;

/** 3消 */
@ccclass('EliminateMatch3UiManager')
export class EliminateMatch3UIManager extends EliminateUIManager {

    @property(EliminateMatch3Logic)
    match3Logic: EliminateMatch3Logic = null;

    onEnable(): void {
        input.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
        input.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
        input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
        input.on(Input.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    }

    onDisable(): void {
        input.off(Input.EventType.TOUCH_START, this.onTouchStart, this);
        input.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
        input.off(Input.EventType.TOUCH_END, this.onTouchEnd, this);
        input.off(Input.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    }

    onTouchStart(event: EventTouch) {
        if (!this.camera) {
            console.log("摄像机为空");
            return;
        }
        const p = event.getLocation();
        let ray = new geometry.Ray();
        this.camera.screenPointToRay(p.x, p.y, ray);
        // this.slotsNode.enabled = false;
        audioManager.playEffect('audio/click');

        const mask = 1 << 1 | 1 << 5 | 1 << 7;
        const maxDistance = 1000000;

        const hasCast = PhysicsSystem.instance.raycastClosest(ray, mask, maxDistance);

        if (hasCast) {
            const res = PhysicsSystem.instance.raycastClosestResult;
            const colliderItem = res.collider;
            const rb1 = colliderItem.node.getComponent(RigidBody);

            // 只过滤玩具

        } else {
            console.log("射线检测没有检测到");
        }
    }

    onTouchMove(event: EventTouch): void {
        console.log("move....");
    }

    onTouchEnd(event: EventTouch): void {

    }
}

